﻿CREATE PROCEDURE [maint].[BackupToDevice]
@DbName [sysname], @CheckSchedule BIT=1, @OutputDiagnostics BIT=0
AS
BEGIN
	SET NOCOUNT ON

	DECLARE @RC int, @ERROR int
	SET @RC = 0

	IF(@OutputDiagnostics = 1)
		BEGIN
			PRINT OBJECT_NAME(@@PROCID)
			PRINT N'	@DbName = N''' + ISNULL(@DbName,N'NULL') + '''';
			PRINT N'	@CheckSchedule = N''' + ISNULL(CAST(@CheckSchedule AS nvarchar),N'NULL') + '''';
		END

	DECLARE @DeviceName sysname
	SET @DeviceName = maint.GetDeviceName(@DbName)
	IF(@DeviceName IS NULL)
		BEGIN
			RAISERROR('%s has no valid device name.
Check the [config].[DbMaintenance] table for maintenance configuration settings.',16,1,@DbName)
			SET @ERROR = @@ERROR
			GOTO ERROR_HANDLER
		END

	--GET DEVICE DETAILS
	DECLARE @BackupSetName sysname, @BackupSetExpireDate datetime
	EXEC maint.GetDeviceBackupSet @DbName, @DeviceName, @BackupSetName OUTPUT, @BackupSetExpireDate OUTPUT, @OutputDiagnostics;

	--FULL BACKUP
	DECLARE @FullRequired bit
	EXEC @RC = maint.IsFullBackupRequired @DbName, @FullRequired OUTPUT, @OutputDiagnostics;
	IF(@RC <> 0) GOTO ERROR_HANDLER

	IF(@BackupSetName IS NULL)
	OR(@BackupSetExpireDate IS NULL)
	OR(@BackupSetExpireDate < CURRENT_TIMESTAMP)
	OR(@FullRequired = 1)
	OR(@CheckSchedule = 0)
		BEGIN		
			DECLARE @BackupSetGroupName sysname
			SET @BackupSetGroupName = maint.GetBackupSetGroupName(@DbName)
			IF(@BackupSetGroupName IS NULL)
				BEGIN
					RAISERROR('%s has no valid backupset group name.
Check the [config].[DbMaintenance] table for maintenance configuration settings.',16,1,@DbName)
					SET @ERROR = @@ERROR
					GOTO ERROR_HANDLER
				END

			SET @BackupSetName = @BackupSetGroupName + N'_' + CONVERT(nchar(8),GETDATE(),112)

			DECLARE @FullExpireDate datetime = DATEADD(week,1,GETDATE());
			IF(@CheckSchedule = 1)
			BEGIN
				EXEC @RC = maint.GetFullBackupExpireDate @DbName, @FullExpireDate OUTPUT, NULL, @OutputDiagnostics
				IF(@RC <> 0) GOTO ERROR_HANDLER
			END

			EXEC @RC = maint.InitialiseDevice @DbName, @DeviceName, @OutputDiagnostics
			IF(@RC <> 0) GOTO ERROR_HANDLER

			EXEC @RC = maint.BackupFullToDevice @DbName, @DeviceName, @BackupSetName, @FullExpireDate, @OutputDiagnostics
			IF(@RC <> 0) GOTO ERROR_HANDLER

			SET @BackupSetExpireDate = @FullExpireDate
		END

	--DIFFERENTIAL BACKUP
	DECLARE @DiffRequired bit
	EXEC @RC = maint.IsDiffBackupRequired @DbName, @BackupSetName, @DiffRequired OUTPUT, @OutputDiagnostics;
	IF(@RC <> 0) GOTO ERROR_HANDLER

	IF (@DiffRequired = 1)
		BEGIN	
			DECLARE @DiffExpireDate datetime
			EXEC @RC = maint.GetDiffBackupExpireDate @DbName, @DiffExpireDate OUTPUT, NULL, @OutputDiagnostics
			IF(@RC <> 0) GOTO ERROR_HANDLER

			EXEC @RC = maint.BackupDiffToDevice @DbName, @DeviceName, @BackupSetName, @DiffExpireDate, @OutputDiagnostics;
			IF(@RC <> 0) GOTO ERROR_HANDLER
		END

	--LOG BACKUP
	DECLARE @LogRequired bit
	EXEC @RC = maint.IsLogBackupRequired @DbName, @LogRequired OUTPUT, @OutputDiagnostics
	IF(@RC <> 0) GOTO ERROR_HANDLER
	
	IF (@LogRequired = 1)
		BEGIN			
			EXEC @RC = maint.BackupLogToDevice @DbName, @DeviceName, @BackupSetName, @BackupSetExpireDate, @OutputDiagnostics;
			IF(@RC <> 0) GOTO ERROR_HANDLER
		END

GOTO EXIT_PROC

ERROR_HANDLER:
	IF(@RC = 0)
		SET @RC = @ERROR

EXIT_PROC:
	RETURN(@RC)
END